﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//请先添加引用
namespace BaoJian.P
{
    public partial class clist : System.Web.UI.Page
    {
        public DataTable dt = null;
        public string columnID = null;       
        int pageSize = 10, RecordCount, PageCount, CurrentPage;
        public string ColumnName = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            columnID = HttpContext.Current.Request.QueryString["C"];

            if (columnID == null)
            {
                Page.Response.Redirect("../Error.aspx");
            }
            GetCurrentPosition();
            if (!Page.IsPostBack)
            {             
                ListBind();
                CurrentPage = 0;
                ViewState["PageIndex"] = 0;

                RecordCount = CalculateRecord();
                lblRecordCount.Text = RecordCount.ToString();

                PageCount = (RecordCount % pageSize > 0) ? (RecordCount / pageSize) + 1 : (RecordCount / pageSize);
                lblPageCount.Text = PageCount.ToString();
                ViewState["PageCount"] = PageCount;
            }
        }

        private void GetCurrentPosition()
        {
            LYQ.BLL.SysObj bll = new LYQ.BLL.SysObj();
            LYQ.Model.SysObj model = bll.GetModel(columnID);
            ColumnName = model.OName;
        }
        public int CalculateRecord()
        {
            string strCount = string.Format("SELECT count(*) as co FROM [VHotExchangeDefault] WHERE  [PColumn]='{0}'", columnID);
            int count = (int)DbHelperSQL.GetSingle(strCount);
            return count;
        }

        public void ListBind()
        {
            int StartIndex;
            StartIndex = CurrentPage * pageSize;     
         
            string strSel = "SELECT * FROM [VHotExchangeDefault] where  [PColumn]='{0}' order by sid desc";
            strSel = string.Format(strSel, columnID);
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(strSel, DbHelperSQL.connectionString);
            da.Fill(ds, StartIndex, pageSize, "ExchangeProduct");

            DataList1.DataSource = ds.Tables["ExchangeProduct"].DefaultView;
            DataList1.DataBind();

            lbnNextPage.Enabled = true;
            lbnPrevPage.Enabled = true;

            if (CurrentPage == (PageCount - 1)) lbnNextPage.Enabled = false;
            if (CurrentPage == 0) lbnPrevPage.Enabled = false;
            lblCurrentPage.Text = (CurrentPage + 1).ToString();
        }

        protected void lbnNextPage_Click(object sender, EventArgs e)
        {
            CurrentPage = (int)ViewState["PageIndex"];
            PageCount = (int)ViewState["PageCount"];
            if (CurrentPage < (PageCount - 1)) CurrentPage++;
            ViewState["PageIndex"] = CurrentPage;
            ListBind();

        }
        protected void lbnPrevPage_Click(object sender, EventArgs e)
        {
            CurrentPage = (int)ViewState["PageIndex"];
            PageCount = (int)ViewState["PageCount"];
            if (CurrentPage > 0) CurrentPage--;
            ViewState["PageIndex"] = CurrentPage;
            ListBind();
        }
    }
}
